package org.mineground.commands.player.admin;

import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import org.mineground.Main;
import org.mineground.PermissionNodes;

public class Mode implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) throws CommandException {
	Player player = (Player) sender;

	if (!player.hasPermission(PermissionNodes.PERMISSION_CREATIVE_TOGGLE)) {
            return true;
        }
        
        Player affectedPlayer = player;
        String action;
        
	if (args.length > 0) {
	    affectedPlayer = Main.getInstance().getServer().getPlayer(args[0]);
            
            if (affectedPlayer == null) {
                player.sendMessage(ChatColor.RED + "* Error: Invalid player name");
                return true;
            }
	}
        
        if (affectedPlayer.getGameMode() == GameMode.CREATIVE) {
            affectedPlayer.setGameMode(GameMode.SURVIVAL);
            action = "disabled";
        }
        
        else {
            affectedPlayer.setGameMode(GameMode.CREATIVE);
            action = "enabled";
        }
        
	if (affectedPlayer.equals(player)) {
            player.sendMessage(ChatColor.DARK_GREEN + "Your creative mode has been " + action);
            return true;
        }
        
        player.sendMessage(ChatColor.DARK_GREEN + affectedPlayer.getDisplayName() + ChatColor.DARK_GREEN + "'s creative mode has been " + action);
        affectedPlayer.sendMessage(ChatColor.DARK_GREEN + "Your creative mode has been " + action);
	return true;
    }
}
